using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Openmerch.Device;
using JlPay.Sdk.Models.Openmerch.Device;

namespace JlPay.Sdk.Examples.Openmerch.Device
{
    /// <summary>
    /// 开放商户 - 设备变更功能使用示例
    /// </summary>
    public class DeviceChangeDemo
    {
        /// <summary>
        /// 构建设备变更请求
        /// </summary>
        private static DeviceChangeRequest BuildDeviceChangeRequest()
        {
            var request = new DeviceChangeRequest
            {
                MerchNo = "84944035812S123",
                TermNo = "S1002513",
                NewDeviceNo = "NS301257964"
            };

            return request;
        }

        /// <summary>
        /// 设备变更示例
        /// </summary>
        public static async Task DeviceChangeDemoAsync()
        {
            Console.WriteLine("开始执行设备变更示例");

            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            var deviceService = new DeviceService(config);

            // 创建设备变更请求
            var request = BuildDeviceChangeRequest();

            try
            {
                // 发送请求
                var response = await deviceService.ChangeDeviceAsync(request);

                // 使用优化的响应处理
                response.HandleResponse("设备变更", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new DeviceChangeResponse();
                response.HandleResponseWithException("设备变更", config, ex);
            }

            Console.WriteLine("设备变更示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 设备变更示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await DeviceChangeDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 设备变更示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 